import mainRoot from "../../../Game/MainRoot";
import { level_cardCompositionCardType, level_cardCompositionE} from "../const/level_cardCompositionConst";
import level_cardComposition from "../level_cardComposition";
import level_cardComposition_cardGroup from "./level_cardComposition_cardGroup";

const {ccclass, property} = cc._decorator;

@ccclass
export default class level_cardComposition_card extends cc.Component {

    @property({type:cc.Node,tooltip:"卡牌节点"})
    cardNode:cc.Node = null;

    @property({type:cc.Node,tooltip:"卡牌显示节点"})
    cardShowNode:cc.Node = null;
    
    @property({type:cc.Node,tooltip:"高亮显示节点"})
    lightNode:cc.Node = null;

    @property({type:cc.SpriteFrame,tooltip:"彩虹图片"})
    rainbowSpf:cc.SpriteFrame = null;

    @property({type:cc.SpriteFrame,tooltip:"普通图片"})
    normalSpf:cc.SpriteFrame = null;

    @property({type:cc.Sprite,tooltip:"牌的背景"})
    cardBg:cc.Sprite = null;

    @property({type:cc.Node,tooltip:"金色背景"})
    goldBg:cc.Node = null;

    /**卡牌点数 */
    cardComposition:level_cardCompositionE = null;

    /**卡牌类型 */
    cardType:level_cardCompositionCardType;

    /**是否被选择 */
    selectFlag:boolean = false;

    showCardSpf:cc.SpriteFrame = null;

    otherCardSpf:cc.SpriteFrame = null;

    group:level_cardComposition_cardGroup = null;

    level:level_cardComposition = null;

    isJokerAni:boolean = false;

    setGroup(group:level_cardComposition_cardGroup){
        this.group = group;
    } 

    getGroup(){
        return this.group;
    }

    initCard(level:level_cardComposition){
        this.showCardSpf = null;
        this.otherCardSpf = null;
        this.stopJokerAni();
        this.selectFlag = false;
        // this.lightNode.active = false;
        this.cardNode.y = 0;
        this.lightNode.y = 0;
        this.level = level;
        this.cardShowNode.color = cc.Color.WHITE;
        this.cardBg.spriteFrame = this.normalSpf;
    }

    bindData(cardComposition:level_cardCompositionE,cardType:level_cardCompositionCardType){
        this.cardComposition = cardComposition;
        this.cardType = cardType;
        this.loadCard();
    }

    /**加载卡牌 */
    loadCard(){
        console.log(this.level.getLevelInfo().levelType,"怎么个事????")
        switch(this.cardType){
            case level_cardCompositionCardType.universal:{
                mainRoot.LevelSystem.getOneTypeBundle(this.level.getLevelInfo().levelType).load(`card/${13}`,cc.SpriteFrame,(err,spf:cc.SpriteFrame)=>{
                    if(err){
                        console.error(err,"加载卡牌失败",err);
                        return;
                    }
                    this.otherCardSpf = spf;
                    this.showCardSpf = spf;
                    this.cardShowNode.getComponent(cc.Sprite).spriteFrame = this.otherCardSpf;
                    this.cardBg.spriteFrame = this.rainbowSpf;
                });
                break;
            }
            case level_cardCompositionCardType.query:{
                mainRoot.LevelSystem.getOneTypeBundle(this.level.getLevelInfo().levelType).load("card/wenhaopai",cc.SpriteFrame,(err,spf:cc.SpriteFrame)=>{
                    if(err){
                        console.error(err,"加载卡牌失败",err);
                        return;
                    }
                    this.otherCardSpf = spf;
                    this.showCardSpf = spf;
                    this.cardShowNode.getComponent(cc.Sprite).spriteFrame = this.otherCardSpf;
                });
                break;
            }
            case level_cardCompositionCardType.normal:{
                mainRoot.LevelSystem.getOneTypeBundle(this.level.getLevelInfo().levelType).load(`card/${this.cardComposition}`,cc.SpriteFrame,(err,spf:cc.SpriteFrame)=>{
                    if(err){
                        console.error(err,"加载卡牌失败",err);
                        return;
                    }
                    this.cardShowNode.getComponent(cc.Sprite).spriteFrame = spf;
                    this.cardShowNode.color = this.level.colorMap[this.cardComposition];
                });
                // console.log(this.cardComposition,">>>>>>>>>>>>");
                break;
            }
            case level_cardCompositionCardType.obstacles:{
                mainRoot.LevelSystem.getOneTypeBundle(this.level.getLevelInfo().levelType).load(`card/${15}`,cc.SpriteFrame,(err,spf:cc.SpriteFrame)=>{
                    if(err){
                        console.error(err,"加载卡牌失败",err);
                        return;
                    }
                    this.cardShowNode.getComponent(cc.Sprite).spriteFrame = spf;
                });
                break;
            }
        }
    }

    /**设置选择 */
    setSelect(selectFlag:boolean){
        this.selectFlag = selectFlag;
        cc.Tween.stopAllByTarget(this.cardNode);
        cc.Tween.stopAllByTarget(this.lightNode);
        // this.lightNode.active = selectFlag;
        this.goldBg.active = selectFlag;
        switch(selectFlag){
            case true:{
                cc.tween(this.lightNode).to(0.1,{y:20}).start();
                cc.tween(this.cardNode).to(0.1,{y:20}).start();
                break;
            }
            case false:{
                // this.lightNode.y = 0;
                cc.tween(this.lightNode).to(0.1,{y:0}).start();
                cc.tween(this.cardNode).to(0.1,{y:0}).start();
                break;
            }
        }
    }

    //放入组中
    onPushToGroup(){
        switch(this.cardType){
            case level_cardCompositionCardType.universal:{
                let curIndex:number = this.group.findCardIndex(this);
                let preIndex:number = curIndex - 1;
                let card = this.group.getCards()[preIndex];
                if(card){
                    if(card.cardType == level_cardCompositionCardType.universal || card.cardType == level_cardCompositionCardType.normal){
                        let composition = card.cardComposition;
                        this.cardComposition = composition;
                        if(composition != null ){
                            mainRoot.LevelSystem.getOneTypeBundle(this.level.getLevelInfo().levelType).load(`card/${composition}`,cc.SpriteFrame,(err,spf:cc.SpriteFrame)=>{
                                if(err){
                                    console.log(err,"加载图片失败");
                                    return;
                                }
                                this.showCardSpf = spf;
                            });
                            this.jokerAni();
                        }
                    }
                }
                break;
            }
        }
    }

    //从组中移除
    onSpliceToGroup(){
        switch(this.cardType){
            case level_cardCompositionCardType.universal:{
                this.stopJokerAni();
                break;
            }
        }
    }

    //开始大王的动画
    jokerAni(){
        if(this.isJokerAni){
            return;
        }
        this.isJokerAni = true;
        let color = this.level.colorMap[this.cardComposition];
        let oneTime:number = 2;
        let sprite = this.cardShowNode.getComponent(cc.Sprite);
        let tween = cc.tween(this.cardShowNode).delay(oneTime).to(0.2,{opacity:0}).call(()=>{
            mainRoot.LevelSystem.getOneTypeBundle(this.level.getLevelInfo().levelType).load(`card/${this.cardComposition}`,cc.SpriteFrame,(err,spf:cc.SpriteFrame)=>{
                if(err){
                    console.log(err,"加载图片失败");
                    return;
                }
                this.showCardSpf = spf;
                sprite.spriteFrame = this.showCardSpf;
                this.cardBg.spriteFrame = this.normalSpf;
                this.cardShowNode.color = color;
            });
        }).to(0.2,{opacity:255}).delay(oneTime).to(0.2,{opacity:0}).call(()=>{
            sprite.spriteFrame = this.otherCardSpf;
            this.cardShowNode.color = cc.Color.WHITE;
            this.cardBg.spriteFrame = this.rainbowSpf;
        }).to(0.2,{opacity:255});
        cc.tween(this.cardShowNode).repeatForever(tween).start();
    }

    //停止大王的动画
    stopJokerAni(){
        this.isJokerAni = false;
        let sprite = this.cardShowNode.getComponent(cc.Sprite);
        cc.Tween.stopAllByTarget(this.cardShowNode);
        this.cardComposition = null;
        sprite.spriteFrame = this.otherCardSpf;
        this.cardShowNode.opacity = 255;
        this.cardShowNode.color = cc.Color.WHITE;
        this.cardBg.spriteFrame = this.rainbowSpf;
    }

    //卡牌翻转
    cardToflip(){
        this.cardType = level_cardCompositionCardType.normal;
        this.loadCard();
    }

}
